using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    private static GameManager instance;

    public static GameManager Instance
    {
        get { return instance; }
    }

    private PackageTable packageTable;

    private void Awake()
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
    }

    // Start is called before the first frame update
    void Start()
    {
        UIManager.Instance.OpenPanel(UIConst.PackagePanel);
    }

    // Update is called once per frame
    void Update()
    {
    }

    public PackageTable GetPackageTable()
    {
        if (packageTable != null) return packageTable;
        packageTable = Resources.Load<PackageTable>("TableData/PackageTable");
        return packageTable;
    }

    public List<PackageLocalItem> GetPackageLocalItems()
    {
        return PackageLocalData.Instance.LoadData();
    }

    public PackageLocalItem GetPackageLocalItemById(int id)
    {
        List<PackageLocalItem> packageLocalItems = GetPackageLocalItems();
        foreach (var packageLocalItem in packageLocalItems)
        {
            if (packageLocalItem.id == id)
            {
                return packageLocalItem;
            }
        }

        return null;
    }

    public PackageTableItem GetPackageTableItemByid(int id)
    {
        List<PackageTableItem> packageTableItems = GetPackageTable().dataList;
        foreach (var packageTableItem in packageTableItems)
        {
            if (packageTableItem.id == id)
            {
                return packageTableItem;
            }
        }

        return null;
    }

    public PackageLocalItem GetPackageLocalItemByUid(string uid)
    {
        List<PackageLocalItem> packageLocalItems = GetPackageLocalItems();
        foreach (var packageLocalItem in packageLocalItems)
        {
            if (packageLocalItem.uid.Equals(uid))
            {
                return packageLocalItem;
            }
        }

        return null;
    }


    public List<PackageLocalItem> SortPackageLocalItem()
    {
        List<PackageLocalItem> packageLocalItems = GetPackageLocalItems();
        packageLocalItems.Sort(new PackageLocalItemComparer());
        return packageLocalItems;
    }

    public void DeleteLocalItem(List<string> uids)
    {
        for (var i = 0; i < uids.Count; i++)
        {
            PackageLocalItem localItem = GetPackageLocalItemByUid(uids[i]);
            PackageLocalData.Instance.RemoveData(localItem);
        }
        PackageLocalData.Instance.SaveData();
        
    }
}

public class PackageLocalItemComparer : Comparer<PackageLocalItem>
{
    public override int Compare(PackageLocalItem a, PackageLocalItem b)
    {
        //先星级排序，在id排序，在等级排序
        PackageTableItem x = GameManager.Instance.GetPackageTableItemByid(a.id);
        PackageTableItem y = GameManager.Instance.GetPackageTableItemByid(b.id);

        int starRes = y.star.CompareTo(x.star);

        if (starRes == 0)
        {
            int idRes = y.id.CompareTo(x.id);
            if (idRes == 0)
            {
                int levelRes = b.level.CompareTo(a.level);

                return levelRes;
            }
                
            return idRes;
        }

        return starRes;
    }
}